#include "Game.h"

IGame* createGame( HINSTANCE hInst, HWND hWnd )
{
	return new Game( hInst, hWnd );
}

void Game::getWindowSettings(int &width, int &height, bool &windowed, bool &minimizebox, bool &maximizebox)
{
	width       = m_scrWidth;
	height      = m_scrHeight;
	maximizebox = 1;
	windowed    = 1;
}

Game::Game( HINSTANCE hInst, HWND hWnd ) :
	m_input( hInst, hWnd ), m_scrHeight(480), m_scrWidth(640)
{
	// initialize camera
	m_camera.hand = Camera::RIGHTHANDED;

	m_camera.eye = float3( 0, 0, 1.0f );
	m_camera.at  = float3( 0, 0, 0 );
	m_camera.up  = float3( 0, 1, 0 );
	
	m_camera.projectionType = Camera::ORTHO;
	m_camera.ortho.h = (float)m_scrHeight;
	m_camera.ortho.w = (float)m_scrWidth;


	m_camera.zn = 0.1f;
	m_camera.zf = 100.0f;

}

Game::~Game()
{
	m_polygonView.destroy();
	m_d3dDevice = 0;
}

void Game::onCreateGraphics( D3DDevice device )
{
	m_d3dDevice = device;
	m_polygonView.initialize( m_d3dDevice );
	m_polygonView.setup( m_polygon, float4( 1.0f, 1.0f, 1.0f, 1.0f ) );

	m_d3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );

	float44 world = identity44();
	float44 view = m_camera.view();
	float44 proj = m_camera.projection();

	m_d3dDevice->SetTransform( D3DTS_WORLD, &world );
	m_d3dDevice->SetTransform( D3DTS_VIEW, &view );
	m_d3dDevice->SetTransform( D3DTS_PROJECTION, &proj );
	
}

void Game::onResetGraphics( D3DDevice device )
{
	m_d3dDevice = device;
	float width  = (float)DXUTGetD3D9BackBufferSurfaceDesc()->Width;
	float height = (float)DXUTGetD3D9BackBufferSurfaceDesc()->Height;

	m_d3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );

	m_camera.ortho.w = width;
	m_camera.ortho.h = height;

	float44 view = m_camera.view();
	float44 proj = m_camera.projection();

	m_d3dDevice->SetTransform( D3DTS_VIEW, &view );
	m_d3dDevice->SetTransform( D3DTS_PROJECTION, &proj );

	onResize(width, height);
}



void Game::onLostGraphics()
{
}

void Game::onFrameMove(float dt)
{
}

void Game::onFrameRender(float dt)
{
	float44 world = identity44();
	m_polygonView.render( world );
}

void Game::onDestroyGraphics()
{
}

void Game::onResize(int w, int h)
{
	m_scrWidth  = w;
	m_scrHeight = h;
}

bool Game::msgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	switch(uMsg) 
	{ 
		case WM_SIZE: 
			if(wParam != SIZE_MINIMIZED)
				onResize(LOWORD(lParam), HIWORD(lParam));
			break;
	}

	return false;
}
